<?php

class Rph extends CI_Controller {
	
	function Rph()
	{
		parent::__construct();
		$this->load->model('rph_model', '', TRUE);
		$this->load->model('Home_model', '', TRUE);
		$this->load->model('auth_model', '', TRUE);
		$this->load->model('lokasi_model', '', TRUE);
		$this->load->model('satuan_model', '', TRUE);
	}
	
	function index()
	{
		if ($this->session->userdata('login') == FALSE)
		{
			redirect('auth');
		}
		// Load default view
		$data['title'] = "Rumah Pemotongan Hewan";
		$data['main_view'] = 'rph/rph_view';
		
		$data['all_rph'] = $this->rph_model->get_all_rph();
		
		$id_role = $this->session->userdata('id_role'); 
		$data['hak_akses0'] = $this->Home_model->get_hak_akses($id_role,0);
		$data['hak_akses1'] = $this->Home_model->get_hak_akses($id_role,1);
		$data['hak_akses2'] = $this->Home_model->get_hak_akses($id_role,2);
		
		$data['all_kota'] = $this->lokasi_model->get_all_kota();
		$data['all_satuan'] = $this->satuan_model->get_all_satuan();
		$all_rph = $this->rph_model->get_all_rph();
		if($all_rph == null)
			$data['all_rph'] = null;
		else
			$data['all_rph'] = $all_rph->result_array();
		$all_kordinat = $this->rph_model->get_all_kordinat();
		$data['all_kordinat'] = $all_kordinat->result_array();
		
		// Load view
		$this->load->view('template', $data);
	}
	
	function add_rph(){
	
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		$tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
	
		$arr = array(
			'NAMA' => $this->input->post('nama'),
			'ALAMAT' => $this->input->post('alamat'),
			'ID_KOTA' => $this->input->post('id_kota'),
			'NO_TELP' => $this->input->post('no_telp'),
			'EMAIL' => $this->input->post('email'),
			'KODE_POS' => $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI' => $tanggal,
			'LAT' => $this->input->post('lat'),
			'LON' => $this->input->post('lng'),
			'INFO' => $this->input->post('info')
		);
		
		$id_titik_pasok = $this->rph_model->insert_titik_pasok($arr);
		$arr2 = array(
			'ID_TITIK_PASOK' => $id_titik_pasok,
			'JUMLAH_PEMOTONGAN_PER_HARI' => $this->input->post('produksi_normal'),
			'JUMLAH_DOKTER_HEWAN' => $this->input->post('jumlah_dokter_hewan'),
			'LISENSI' => $this->input->post('lisensi'),
			'JUMLAH_JURU_SEMBELIH' => $this->input->post('jumlah_juru_sembelih'),
			'JUMLAH_KEURMASTER' => $this->input->post('jumlah_keurmaster')
		);
		
		if($this->rph_model->insert_rph($arr2)){
			redirect("/rph");
		} else {
			echo "gagal";
		}
		/*print_r($arr);
		print_r($arr2);*/
		
		
	}
	
	function update_rph(){
		$tanggal = $this->input->post('tanggal_berdiri');
		$tanggal_arr = explode('/', $tanggal);
		if(count($tanggal_arr)>1){
			$tanggal = $tanggal_arr[2]."-".$tanggal_arr[0]."-".$tanggal_arr[1];
		}
		$id = $this->input->post("id-rph");
		$arr = array(
			'NAMA' 				=> $this->input->post('nama'),
			'ALAMAT' 			=> $this->input->post('alamat'),
			'ID_KOTA' 			=> $this->input->post('id_kota'),
			'NO_TELP' 			=> $this->input->post('no_telp'),
			'EMAIL' 			=> $this->input->post('email'),
			'KODE_POS' 			=> $this->input->post('kode_pos'),
			'TANGGAL_BERDIRI'	=> $tanggal,
			'LAT' 				=> $this->input->post('lat'),
			'LON' 				=> $this->input->post('lng'),
			'INFO' 				=> $this->input->post('info')
		);
		if($this->rph_model->update_titik_pasok($id, $arr)){
			$arr2 = array(
				'JUMLAH_PEMOTONGAN_PER_HARI'=> $this->input->post('produksi_normal'),
				'JUMLAH_DOKTER_HEWAN' 		=> $this->input->post('jumlah_dokter_hewan'),
				'LISENSI'	 				=> $this->input->post('lisensi'),
				'JUMLAH_JURU_SEMBELIH' 		=> $this->input->post('jumlah_juru_sembelih'),
				'JUMLAH_KEURMASTER' 		=> $this->input->post('jumlah_keurmaster')
			);
			if($this->rph_model->update_rph($id, $arr2)){
				redirect("/rph");
			} else {
				echo "gagal";
			}
		}
		
	}
	
	function delete_rph(){
		$id_pasok = $this->input->post('id_titik_pasok');
		if($this->rph_model->delete_rph($id_pasok)){
			if($this->rph_model->delete_titik_pasok($id_pasok)){
				echo json_encode(true);
			} else {
				echo json_encode(false);
			}
		}
	}
	
}